package com.ctcemti.mdm.project.sys.service.impl;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.project.sys.dao.InfDisciplinesMapper;
import com.ctcemti.mdm.project.sys.dao.ResEntElementTreeMapper;
import com.ctcemti.mdm.project.sys.entity.InfDisciplines;
import com.ctcemti.mdm.project.sys.service.InfDisciplinesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName InfDisciplinesMapper
 * @Description 专业领域逻辑层接口层
 * @Author bxf
 * @Date 11:20 2019/7/12
 * @Version V1.0
 **/
@Service
public class InfDisciplinesServiceImpl implements InfDisciplinesService {

    @Autowired
    private InfDisciplinesMapper infDisciplinesMapper;
    @Autowired
    private ResEntElementTreeMapper resEntElementTreeMapper;

    /**
     * @return
     * @Author pzw
     * @Description 获取所有领域
     * @Date 11:39 2019/7/18
     * @Param
     **/
    @Override
    public List<InfDisciplines> selectAllMenu(InfDisciplines infDisciplines) {
        Example example = new Example(InfDisciplines.class);
        Example.Criteria type = example.createCriteria();
        if(infDisciplines.getType()!=null&&infDisciplines.getType()!=0){
            type.andEqualTo("type", infDisciplines.getType());
        }
        if(infDisciplines.getPid()!=null&&infDisciplines.getPid()!=0){
            type.andEqualTo("pid",infDisciplines.getPid());
        }
        if(infDisciplines.getName()!=null&&infDisciplines.getName()!=""){
            type.andLike("name","%"+infDisciplines.getName()+"%");
        }
        return infDisciplinesMapper.selectByExample(example);
    }

    /**
     * @param infDisciplines
     * @return
     * @Author pzw
     * @Description 新增
     * @Date 11:39 2019/7/18
     * @Param
     */
    @Override
    @Transactional
    public int insertInfDisciplines(InfDisciplines infDisciplines) {
        return infDisciplinesMapper.insertSelective(infDisciplines);
    }

    /**
     * @param infDisciplines
     * @return
     * @Author pzw
     * @Description 根据id查询数据
     * @Date 11:39 2019/7/18
     * @Param
     */
    @Override
    public InfDisciplines queryInfDisciplinesByID(InfDisciplines infDisciplines) {
        InfDisciplines infDisciplines1 = infDisciplinesMapper.selectByPrimaryKey(infDisciplines);
        return infDisciplines1;
    }

    /**
     * @param infDisciplines
     * @return
     * @Author pzw
     * @Description 修改
     * @Date 11:39 2019/7/18
     * @Param
     */
    @Override
    @Transactional
    public int updateInfDisciplines(InfDisciplines infDisciplines) {
        return infDisciplinesMapper.update(infDisciplines);
    }

    /**
     * @param ids
     * @return
     * @Author pzw
     * @Description 删除
     * @Date 11:39 2019/7/18
     * @Param
     */
    @Override
    @Transactional
    public Result<InfDisciplines> deleteInfDisciplines(Integer[] ids,Integer type) {
        int count=infDisciplinesMapper.hasEBSTree(ids);
        if(count>0){
            return  new Result<InfDisciplines>(ResultEnum.ZTSJ_1100,null);
        }
        if(type==2){
            String ids_str = StringUtils.join(ids, ",");
            int num = infDisciplinesMapper.deleteByIds(ids_str);
            return new Result<InfDisciplines>(ResultEnum.SUCCESS,null);
//            resEntElementTreeMapper.deleteEBSTreeByDisciplineId(ids);
//            return resEntElementTreeMapper.deleteEBSTreeMethodsByDisciplineId(ids);
        }
        //级联删除
        int num =  infDisciplinesMapper.deleteToCascade(ids);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,null);

//        resEntElementTreeMapper.deleteEBSTreeByDisciplineId(ids);
//        return resEntElementTreeMapper.deleteEBSTreeMethodsByDisciplineId(ids);
    }

    /**
     * @param infDisciplines
     * @return
     * @Author pzw
     * @Description 二级树节点条件查询
     * @Date 11:39 2019/7/18
     * @Param
     */
    @Override
    public List<InfDisciplines> selectAllInfDisciplinesByTree(InfDisciplines infDisciplines) {
        return infDisciplinesMapper.selectAllInfDisciplinesByTree(infDisciplines);
    }

    /**
     * 根据条件查询列表
     *
     * @param infDisciplines
     * @return
     */
    @Override
    public List<InfDisciplines> queryInfDisciplinesList(InfDisciplines infDisciplines) {
        return infDisciplinesMapper.select(infDisciplines);
    }
}